<?php

namespace app\controller;

//建筑装饰控制器
use app\service\UserOrnamentsService;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserOrnamentsController extends Base
{

    /**
     * 获取用户装饰列表
     */
    public function list(): Response
    {
        try {
            $res = (new UserOrnamentsService())->list($this->user_id);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
        return $this->json($res);
    }

    /**
     * 购买装饰
     */
    public function buy(): Response
    {
        $ornament_id = $this->request->post('ornament_id');
        try {
            $res = (new UserOrnamentsService())->buy($this->user_id, $ornament_id);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
        return $this->json($res);
    }

    /**
     * 安装装饰
     */
    public function install(): Response
    {
        $user_ornament_id = $this->request->post('user_ornament_id');
        $position = $this->request->post('position');
        try {
            $res = (new UserOrnamentsService())->install($this->user_id, $user_ornament_id, $position);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
        return $this->json($res);
    }

    /**
     * 卸载装饰
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function uninstall(): Response
    {
        $user_ornament_id = $this->request->post('user_ornament_id');
        $res = (new UserOrnamentsService())->uninstall($this->user_id, $user_ornament_id);
        return $this->json($res);

    }


}